<?php

class Core_Table_Foundation_Industry extends Kd_Data_Table {

    private $_relationTable = '{t}foundation_industry_relation';

    public function __construct() {
        parent::__construct();
    }

    public function getRelations(array $codes) {
        return Kd_Data_Factory::getQueryDcl()
                ->from($this->_relationTable)
                ->where()->contain('industry_code', ':industryCodes:s')->closest()
                ->addParameter('industryCodes', $codes)
                ->setDbDriver($this->getDbDriver())
                ->fetch();
    }

    public function insertRelation($industryCode, array $parents, $transaction = NULL) {
        $insert = Kd_Data_Factory::getInsertDcl()
                        ->into($this->_relationTable)
                        ->value('industry_code', $industryCode)
                        ->value('parent_code', ':parentCode:s')
                        ->value('code_path', ':codePath:s')
                        ->toSql();
        $driver = $this->getDbDriver($transaction);
        foreach ($parents as $parent) {
            $driver->setSql($insert, $parent)->execute();
        }
    }

    public function deleteRelation($industryCode, $transaction = NULL) {
        Kd_Data_Factory::getDeleteDcl()
                ->from($this->_relationTable)
                ->where()->eq('industry_code', $industryCode)->closest()
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();
    }

}